﻿Imports AjaxControlToolkit

Public Class Log
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        'check authentication
        If AuthenticationValidator.CheckIfAuthorized(Me, "Log") = False Then
            Me.Response.Redirect("AccessDeniedMsg.aspx")
        End If

        If Me.IsPostBack = False Then
            Refresh_AccessRecords()
        End If

    End Sub

    Protected Sub filter_Button_Click(sender As Object, e As EventArgs) Handles filter_Button.Click
        Refresh_AccessRecords()
    End Sub

    <System.Web.Services.WebMethodAttribute(), System.Web.Script.Services.ScriptMethodAttribute()> _
    Public Shared Function GetCustomerCompletionList(ByVal prefixText As String, _
                                             ByVal count As Integer, _
                                             ByVal contextKey As String) As String()
        'get customers
        Dim db As New DbHelper
        Dim customers As Collections.ObjectModel.Collection(Of CustomerType) = db.GetCustomersList
        Dim sCustomers = (From c As CustomerType In customers _
                          Where (c.IdNumber.ToLower.Contains(prefixText.ToLower)) _
                             Or (c.Name.ToLower.Contains(prefixText.ToLower)) _
                             Or (c.Telephone.ToLower.Contains(prefixText.ToLower)) _
                          Order By c.Name _
                          Select c).Take(count)

        Dim res As New List(Of String)
        If (sCustomers IsNot Nothing) _
            AndAlso (sCustomers.Count > 0) Then
            Dim cArray(sCustomers.Count)() As String
            For i As Integer = 0 To sCustomers.Count - 1
                Dim c As CustomerType = sCustomers(i)
                Dim cusItem As String = AutoCompleteExtender.CreateAutoCompleteItem(c.Text, c.CustomerId.ToString)
                res.Add(cusItem)
            Next
        End If
        Return res.ToArray

    End Function

    Private Sub Refresh_AccessRecords()

        Dim customerId As Guid
        Dim fromDate As DateTime
        Dim toDate As DateTime

        If (Me.hdCustID.Value IsNot Nothing) _
            AndAlso (Me.hdCustID.Value <> "") Then
            customerId = New Guid(Me.hdCustID.Value)
        Else
            customerId = Guid.Empty
        End If
        If (fromDate_TextBox.Text <> "") _
                AndAlso (IsDate(fromDate_TextBox.Text)) Then
            fromDate = fromDate_TextBox.Text
        Else
            fromDate = DateTime.MinValue
        End If
        If (toDate_TextBox.Text <> "") _
                    AndAlso (IsDate(toDate_TextBox.Text)) Then
            toDate = toDate_TextBox.Text
        Else
            toDate = DateTime.MinValue
        End If

        Dim dt As New DataTable
        If (customerId <> Guid.Empty) _
            AndAlso ((fromDate <> DateTime.MinValue) _
                     OrElse (toDate <> DateTime.MinValue)) Then
            'all fields
            dt = Refresh_AccessRecords_Both(customerId, fromDate, toDate)
            accessRecords_GridView.DataSource = dt
            accessRecords_GridView.DataBind()
            info_Label.Text = "Total " & dt.Rows.Count & " records displayed"
        ElseIf (customerId <> Guid.Empty) _
            AndAlso ((fromDate = DateTime.MinValue) _
                     OrElse (toDate = DateTime.MinValue)) Then
            'customer only
            dt = Refresh_AccessRecords_CustomerOnly(customerId)
            accessRecords_GridView.DataSource = dt
            accessRecords_GridView.DataBind()
            info_Label.Text = "Total " & dt.Rows.Count & " records displayed"
        ElseIf (customerId = Guid.Empty) _
                AndAlso ((fromDate <> DateTime.MinValue) _
                         OrElse (toDate <> DateTime.MinValue)) Then
            'both dates only
            dt = RefreshAccessRecords_DateOnly(fromDate, toDate)
            accessRecords_GridView.DataSource = dt
            accessRecords_GridView.DataBind()
            info_Label.Text = "Total " & dt.Rows.Count & " records displayed"
        Else
            info_Label.Text = "Either Customer, or a date range should be selected"
        End If

    End Sub

    Private Function Refresh_AccessRecords_CustomerOnly(CustomerId As Guid) As DataTable
        Dim db As New DbHelper
        Return db.GetAccessRecordList(CustomerId)
    End Function
    Private Function RefreshAccessRecords_DateOnly(fromDate As DateTime, toDate As DateTime) As DataTable
        Dim db As New DbHelper
        Return db.GetAccessRecordList(fromDate, toDate)
    End Function
    Private Function Refresh_AccessRecords_Both(CustomerId As Guid, fromDate As DateTime, toDate As DateTime) As DataTable
        Dim db As New DbHelper
        Return db.GetAccessRecordList(CustomerId, fromDate, toDate)
    End Function




End Class